package com.example.wan_android.viewmodel

import android.content.Intent
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.wan_android.R
import com.example.wan_android.http.ApiService
import com.example.wan_android.http.InMemoryCookieJar
import com.example.wan_android.http.RetrofitClient
import com.example.wan_android.http.bean.UserResponseData
import com.example.wan_android.http.code.UserRepository
import com.example.wan_android.model.DrawItemData
import com.example.wan_android.ui.activity.AboutUsActivity
import com.example.wan_android.ui.activity.CollectionActivity
import com.example.wan_android.ui.activity.FriendWebActivity
import com.example.wan_android.ui.activity.LoginActivity
import com.example.wan_android.ui.activity.SearchActivity
import kotlinx.coroutines.launch

class MainViewModel : BaseViewModel() {

    private val _indexLiveData = MutableLiveData<Int>(0)
    val indexLiveData = _indexLiveData
    private val _openDialog = MutableLiveData<Boolean>(false)
    val openDialog = _openDialog
    private val _indexTitle = MutableLiveData<String>("首页")
    val indexTitle = _indexTitle

    fun setIndexTitle(title: String) {
        _indexTitle.value = title
    }

    val drawItemList: List<DrawItemData> =
        listOf(
            DrawItemData("我的收藏", R.drawable.ic_coin) {
                startActivity(Intent(), targetClass = CollectionActivity::class.java)
            },
            DrawItemData("关于我们", R.drawable.ic_about) {
                startActivity(Intent(), targetClass = AboutUsActivity::class.java)
            },
            DrawItemData("退出登录", R.drawable.ic_exit) {
                _openDialog.value = true
            }
        )
    val noLoginDrawItemList =
        listOf(
            DrawItemData("我的收藏", R.drawable.ic_coin) {
                startActivity(Intent(), targetClass = LoginActivity::class.java)
            },
            DrawItemData("关于我们", R.drawable.ic_about) {
                startActivity(Intent(), targetClass = AboutUsActivity::class.java)
            }
        )


    fun exitLogin() {
        viewModelScope.launch {
            UserRepository().fetchExitLogin()
            // 清除Cookie
            (RetrofitClient.okHttpClient.cookieJar as InMemoryCookieJar).clear()
            _isLogin.value = false
            jumpLoginActivity()
            _openDialog.value = false

        }
    }


    fun setIndex(index: Int) {
        _indexLiveData.value = index
    }


    fun jumpFriendActivity() {
        startActivity(Intent(), targetClass = FriendWebActivity::class.java)
    }

    fun jumpSearchActivity() {
        startActivity(Intent(), targetClass = SearchActivity::class.java)
    }
}